package cn.shyouchi.wuguanjia.android.ui.event.checknum;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class EventCheckNumActivity extends BaseActivity implements CheckNumContract.View {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_check_num)
    RecyclerView mRvCheckNum;
    @BindView(R.id.btn_add_check_num)
    Button mBtnAddCheckNum;

    String projectId;
    String lineId;
    CheckNumPresenter mCheckNumPresenter;

    CheckNumAdapter mCheckNumAdapter;

    boolean needRefresh = true;

    List<CheckNumEntity.ResultMessageBean> mCheckNumEntities = new ArrayList<>();

    @Override
    public int initContentView() {
        return R.layout.activity_event_check_num;
    }

    @Override
    public void initInjector() {
        mCheckNumPresenter = new CheckNumPresenter(mContext, this);
        projectId = getIntent().getStringExtra("projectId");
        lineId = getIntent().getStringExtra("lineId");


    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        EventBus.getDefault().register(this);
        mCheckNumAdapter = new CheckNumAdapter(mCheckNumEntities, this);
        mRvCheckNum.setLayoutManager(new LinearLayoutManager(mContext));
        mRvCheckNum.setAdapter(mCheckNumAdapter);


        View view = LayoutInflater.from(mContext).inflate(R.layout.empty_view_layout, null);

        mCheckNumAdapter.setEmptyView(view);

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @OnClick(R.id.btn_add_check_num)
    public void onClick() {
        Intent intent = new Intent(mContext, AddEventCheckNumActivity.class);
        intent.putExtra("projectId", projectId);
        intent.putExtra("lineId", lineId);
        startActivity(intent);
    }


    @Override
    public void onDataRefresh(List<CheckNumEntity.ResultMessageBean> resultMessageBeanList) {
        mCheckNumEntities.clear();
        mCheckNumEntities.addAll(resultMessageBeanList);
        mCheckNumAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(CheckNumEntity.ResultMessageBean bean) {
        LogUtil.json(bean);

        Intent intent = new Intent(mContext, PreviewCheckNumActivity.class);
        intent.putExtra("checknum", bean);
        intent.putExtra("projectId", projectId);
        startActivity(intent);
    }

    @Override
    public void onDelClick(CheckNumEntity.ResultMessageBean bean) {
        mCheckNumPresenter.delCheckNum(projectId, String.valueOf(bean.getId()));
    }

    @Override
    public void onEditClick(CheckNumEntity.ResultMessageBean bean) {

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
            mCheckNumPresenter.getCheckNumList(lineId);
            needRefresh = false;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChange(DataChangeEvent event) {
        needRefresh = true;
    }

}
